import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import Keys
from selenium.webdriver.common.action_chains import ActionChains


# 跑通第一条用例代码
driver=webdriver.Edge()
driver.maximize_window()
driver.get('https://www.baidu.com/')
# 隐式等待
driver.implicitly_wait(1)

# 点击登录  1、XPATH定位方法
sbcz=driver.find_element(By.XPATH,'//*[@id="s-top-loginbtn"]')
time.sleep(3)
ActionChains(driver).click(sbcz).perform()
# 强制等待
time.sleep(5)
# 输入手机号  2、NAME定位方法  键盘操作
a=driver.find_element(By.XPATH,'//*[@id="TANGRAM__PSP_11__userName"]')
a.send_keys("13103053968")
a.send_keys(Keys.ENTER)
time.sleep(5)
# 输入密码   3、ID定位方法
driver.find_element(By.ID,'TANGRAM__PSP_11__password').send_keys("baidu123aaa")
time.sleep(5)
# 勾选协议
driver.find_element(By.XPATH,'//*[@id="TANGRAM__PSP_11__isAgree"]').click()
time.sleep(3)
# 点击登录按钮
WebDriverWait(driver,0.5).until(EC.presence_of_element_located((By.XPATH,'//*[@id="TANGRAM__PSP_11__submit"]'))).click()
# 输入框搜索
driver.find_element(By.CSS_SELECTOR,'.chat-textarea').send_keys("昌平区天气")
time.sleep(5)
# 搜索按钮
driver.find_element(By.XPATH,'//*[@id="chat-submit-button"]').click()
time.sleep(5)
# JS/滑动操作
driver.execute_script('window srcollTo(0,500)')
# 40
driver.find_element(By.XPATH,'//*[@id="1"]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div[2]/a/span').click()
time.sleep(5)
# 浏览器事件
driver.refresh()

